#include "form_fwjg.h"
#include "printer.h"
#include "qevent.h"
#include "ui_form_fwjg.h"
#include <QIntValidator>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QMenu>
#include <QFileDialog>
Form_fwjg::Form_fwjg(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_fwjg),
    fwjgNumSet(100)
{
    ui->setupUi(this);
    //设置tableWidget不可直接编辑
    ui->tableWidget_cyz->setEditTriggers(QAbstractItemView::NoEditTriggers);//
    ui->tableWidget_set->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //设置表格连接周边
    ui->tableWidget_set->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_cyz->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_cyz->verticalHeader()->setVisible(false);
    ui->tableWidget_set->verticalHeader()->setVisible(false);
    //设置格式
    QIntValidator* validator =new QIntValidator(2, 100,ui->lineEdit_join_count);
    ui->lineEdit_join_count->setValidator(validator);
    QRegularExpression regExp("^([1-9][0-9]*?\\s)+?[1-9][0-9]*?$"); // 正则表达式，只允许输入数字和空格
    QRegularExpressionValidator* expvalidator = new QRegularExpressionValidator(regExp, ui->lineEdit_join_id);
    ui->lineEdit_join_id->setValidator(expvalidator);
    ui->lineEdit_join_id->setContextMenuPolicy(Qt::CustomContextMenu);
    for(int i=0;i<100;++i){
        fwjgNumSet[i]=i+1;
    }
    fun=[this](std::string s){textEdit->append(QString::fromStdString(s));};
    setWindowModality(Qt::ApplicationModal);
    setWindowIcon(QIcon(":/icon/shared.png"));
}

Form_fwjg::~Form_fwjg()
{
    delete ui;
}

void Form_fwjg::setEditTriggersForColumn(QTableWidget* tWid,int c,Qt::ItemFlag flag){
    int c_count=tWid->columnCount();
    if(c>c_count)
        return;
    for(int i=0;i<participantCount;++i){
        tWid->item(i,c)->setFlags(flag);
    }
}

void Form_fwjg::on_pushButton_join_is_clicked()
{
    auto join_count=ui->lineEdit_join_count->text();
    Printer* looger=Printer::init();
    if(imName.isEmpty()){
        looger->print(this->textEdit,"未选择图像！");
        return;
    }
    if(join_count.isEmpty()){
        looger->print(this->textEdit,"参与者人数不能小于2人！");
        return;
    }
    participantCount=join_count.toInt();
    for(int i=0;i<participantCount;++i){
        auto str_count=QString::number(i+1);
        ui->tableWidget_cyz->setItem(i,0,new QTableWidgetItem(str_count));
        ui->tableWidget_cyz->setItem(i,from_ui_name,new QTableWidgetItem(QString("P")+str_count));
        ui->tableWidget_cyz->setItem(i,from_ui_yztxname,new QTableWidgetItem(imName+"_part"+str_count+"."+this->suffix));
    }
    ui->tableWidget_cyz->setEditTriggers(QAbstractItemView::CurrentChanged);
    setEditTriggersForColumn(ui->tableWidget_cyz,0);
}

void Form_fwjg::on_pushButton_fwjg_is_clicked()
{
    auto join_id_text=ui->lineEdit_join_id->text();
    auto logger=Printer::init();
    if(participantCount<=0){
        logger->print(this->textEdit,"未设置参与人数！");
        return;
    }
    if(join_id_text.isNull()){
        logger->print(this->textEdit,"授权集合不能为空！");
        return;
    }
    auto s_list=join_id_text.split(" ");
    for(auto& tem:s_list){
        if(tem.toInt()> participantCount){
            logger->print(this->textEdit,"编号超出范围！");
            return;
        }
    }
    if(accessStructure.count(s_list)){
        logger->print(this->textEdit,"授权集合已经存在！");
        return;
    }
    fwjgCount+=1;
    accessStructure.append(s_list);
    ui->tableWidget_set->insertRow(fwjgNumSet.front()-1);
    ui->tableWidget_set->setItem(fwjgNumSet.front()-1,0,new QTableWidgetItem(QString::number(fwjgNumSet.front())));
    ui->tableWidget_set->setItem(fwjgNumSet.front()-1,1,new QTableWidgetItem(join_id_text));
    fwjgNumSet.removeFirst();
}

void Form_fwjg::closeEvent(QCloseEvent *event)
{
    if(event)
        emit closeSignal();
}


void Form_fwjg::on_pushButton_qx_clicked()
{
    auto tem=ui->tableWidget_cyz->item(0,0);
    if(!tem){
        return;
    }
    ui->tableWidget_cyz->clearContents();
    ui->tableWidget_set->clearContents();
    participantCount=0;
    accessStructure.clear();
    fwjgCount=0;
}


void Form_fwjg::on_pushButton_qx_2_clicked()
{
    if(fwjgCount==0){
        return;
    }
    ui->tableWidget_set->clearContents();
    accessStructure.clear();
    fwjgCount=0;
}


void Form_fwjg::on_tableWidget_set_cellClicked(int row, int column)
{
    if(fwjgNumSet.count(row)==0&&column<1){
        QPoint globalPos = QCursor::pos();
        // 创建自定义上下文菜单
        QMenu contextMenu;
        // 添加菜单项
        QAction* action1 = contextMenu.addAction("删除");
        action1->setIcon(QIcon(":/icon/delete1.png"));
        // 显示菜单，并获取用户选择的菜单项
        QAction* selectedItem = contextMenu.exec(globalPos);
        // 根据用户选择的菜单项执行相应操作
        if (selectedItem == action1) {
            // 执行菜单项1的操作
            fwjgCount-=1;
            accessStructure.erase(accessStructure.cbegin()+row);
            ui->tableWidget_set->removeRow(row);
            fwjgNumSet.push_front(row+1);
        }
    }
}

void Form_fwjg::on_pushButton_mx_is_clicked()
{
    Printer* logger=Printer::init();
    if(participantCount<=0){
        logger->print(this->textEdit,"未设置参与人数！");
        return;
    }
    int t=ui->lineEdit_mx->text().toInt();
    if(t>participantCount){
        logger->print(this->textEdit,"门限值大于参与人数！");
        return;
    };
}


void Form_fwjg::on_pushButton_fwjg_shared_clicked()
{
    emit sharedSignal();
}

void Form_fwjg::on_pushButton_car_clicked()
{
    carIamgePos=QFileDialog::getOpenFileName(nullptr,"选择载体图片","","载体图片 (*.png *.jpg *.jpeg *.bmp *.pgm *.pbm)");
    ui->label_car_pos->setText(carIamgePos);
}
